package com.ld.security.common.mybatis.wrapper;

import com.ld.security.common.core.base.OrderByPo;
import com.ld.security.common.core.base.QueryPo;
import com.ld.security.common.core.base.SuperPo;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.mybatis.core.wrapper.EntityWrapper;
import com.ld.security.common.mybatis.core.wrapper.WrapperFactory;


public class SimpWrapperFactory implements WrapperFactory {


    @Override
    public <V> EntityWrapper<V> getWrapper(SuperPo po) {

        EntityWrapper ew ;
        SuperPo q = po;
        if(po instanceof QueryPo){
            QueryPo<SuperPo> qo = (QueryPo) po;
            q =   qo.getQuery();
        }
        if(q != null){
           ew =  EntityWrapper.of(q);
        }else{
            ew =  EntityWrapper.of();
        }

        return  ew;
    }


    protected void orderBy(EntityWrapper ew,SuperPo po){
        if (po instanceof OrderByPo){
            OrderByPo orderByPo =(OrderByPo) po;
            if(StringUtil.isNotNull(orderByPo.getOrderBy())){
                String columnName = StringUtil.javaToColumn(orderByPo.getOrderBy());
                if(orderByPo.isAsc()){
                    ew.orderByAsc(columnName);
                }else{
                    ew.orderByDesc(columnName);
                }
            }

        }
    }




}
